package com.cy.controller;

import com.cy.bfzcommons.pojo.Category;
import com.cy.bfzcommons.service.CategoryService;
import com.cy.bfzcommons.web.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
@CrossOrigin

public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增商品类型
     * @param category
     * @return
     */
    @PostMapping
    public JsonResult saveCategory(@RequestBody  @ApiParam(name="category",value="商品分类pojo对象",required=true) Category category){
        categoryService.saveCategory(category);
        return JsonResult.ok();
    }

//    @GetMapping("/find")
//    public JsonResult findCategory(){
//        List<Category> list = categoryService.findPicture();
//        return JsonResult.success(list);
//    }

    /**
     * 查询所有的商品分类,按层级封装
     * @return
     */

    @GetMapping("{level}")
    public JsonResult getCategory(@ApiParam(name="level",value="分类级别",required=true) @PathVariable Integer level){
        List<Category> list = categoryService.getCategory(level);
        return JsonResult.ok(list);
    }

    /**
     * 修改商品分类，修改分类的状态和名字
     * @param category
     * @return
     */

    @PutMapping
    public JsonResult updateCategory( @ApiParam(name="category",value="商品分类pojo对象",required=true)  @RequestBody Category category){
        System.out.println(category);
        categoryService.updateCategory(category);
        return JsonResult.ok();
    }

    /**
     * 删除分类以及该分类下的所有子类
     * @param categoryId
     * @return
     */

    @DeleteMapping()
    public JsonResult daleteCategory(@ApiParam(name="categoryId",value="商品分类id",required=true) Integer categoryId,Integer level){
        categoryService.deleteCategory(categoryId,level);
        return JsonResult.ok("nice");
    }




}
